package note.recursionandbacktracking;

/**
 * @program: LeetCodePractice
 * @description: 排列问题
 * @author: WongKee
 * @create: 2020-08-26 14:25
 *
 *
 **/
public class Permutations {
    public static void main(String[] args) {
        int[] arr={1,3,2};
         perm(arr,0,2);
    }

    public static  void perm(int[] arr,int k,int m){
        if(k==m){
            for(int e:arr){
                System.out.print(e+" ");
            }
            System.out.println();
        }else{
            //此时要填充的位置是k
            for(int i=k;i<=m;i++){
                //将[k,n-1]区间的数字依次与k位置进行交换
                swap(arr,k,i);

                //递归求解[k+1,n+1]的全排列
                perm(arr,k+1,m);

                //回溯
                swap(arr,k,i);
            }
        }
    }

    private static void swap(int[] arr, int k, int i) {
        int temp=arr[k];
        arr[k]=arr[i];
        arr[i]=temp;
    }
}
